define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'goods.goods2/index',
        add_url: 'goods.goods2/add',

    };
    var upload = layui.upload;
    var form = layui.form;
    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh', [{
                    text: '添加入库',
                    method: 'none',
                    class: 'layui-btn layui-btn-xs layui-btn-normal addToLibrary',
                    extend: 'data-full="true"',
                }]],
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'ID', search: false},
                    {field: 'price_low', title: '价格最小值', hide: true},
                    {field: 'price_high', title: '价格最大值', hide: true},
                    {field: 'shop_name', title: '店铺名'},
                    {field: 'category_name', title: '分类', fieldAlias: 'gc.name'},
                    {field: 'name', title: '商品名称', fieldAlias: 'g.name'},
                    {field: 'show_price', title: '展示价格', search: false},

                    // {
                    //     field: 'status',
                    //     title: '状态',
                    //     templet: ea.table.switch,
                    //     selectList: {0: '禁用', 1: '启用'},
                    //     fieldAlias: 'g.status'
                    // },

                    /*{field: 'sku_json', title: '规格选项'},*/
                    // {
                    //     field: 'sort', title: '排序值', edit: 'text', search: false, sort: function () {
                    //         console.log(111)
                    //     }
                    // },
                    {field: 'img', title: '商品主图', imageHeight: 80, templet: ea.table.image, search: false},
                    {field: 'sales', title: '销量', search: false, edit: 'text'},
                    {
                        field: 'stock', title: '库存', search: false, templet: function (k, v) {
                            return '<span class="search_stock" id=' + k.id + '>' + k.total_stock + '</span>';
                        }
                    },
                    // {field: 'is_sale', title: '在售状态', templet: ea.table.switch, selectList: {0: '下架', 1: '上架'}},
                    // {field: 'create_time', title: '添加时间',search: 'range'},
                    // {
                    //     width: 250, title: '操作', templet: ea.table.tool,
                    //     operat: [
                    //         'edit',
                    //         [{
                    //             text: '图集',
                    //             url: init.images_url,
                    //             method: 'open',
                    //             fieldExtra: {'goods_id': 'id'},
                    //             auth: 'images',
                    //             class: 'layui-btn layui-btn-xs layui-btn-normal',
                    //             extend: 'data-full="true"',
                    //         }],
                    //         'delete']
                    // },
                ]],
                done: function () {
                    $(".layui-table-body>table>tbody>tr").each(function (index, val) {
                        $($(".layui-table-fixed .layui-table-body table tr")[index]).height($(val).height());
                    });

                    $(".search_stock").click(function () {
                            var id = $(this).attr('id')
                            $.ajax({
                                url: 'stockList',
                                type: 'get',
                                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                                dataType: "json",
                                headers: "{'X-CSRF-TOKEN': window.CONFIG.CSRF_TOKEN}",
                                data: {id: id},
                                timeout: 3000,
                                success: function (res) {
                                    var html = '<table class="layui-table">'
                                        + '<colgroup>'
                                        + '<col width="250">'
                                        + '<col width="100">'
                                        + '</colgroup>'
                                        + '<thead>'
                                        + '<tr>'
                                        + '<th>规格名称</th>'
                                        + '<th>库存</th>'
                                        + '</tr> '
                                        + '</thead>'
                                        + '<tbody>';
                                    res.data.map(function (item, index) {
                                        html += '<tr>'
                                            + '<td>' + item.sku_json[0].name + ':' + item.sku_json[0].value + '</td>'
                                            + '<td>' + item.stock + '</td>'
                                            + '</tr>'
                                    })
                                    html += '</table>'
                                    layer.open(
                                        {
                                            type: 1,
                                            content: html,
                                        }
                                    )
                                },
                                error: function (xhr, textstatus, thrown) {

                                },
                                complete: function () {
                                    // @todo 刷新csrf-token
                                }
                            });
                        }
                    );
                    $(".addToLibrary").click(function () {
                        var tableId = init.table_render_id;
                        var checkStatus = layui.table.checkStatus(tableId),
                            data = checkStatus.data;
                        if (data.length <= 0) {
                            layer.msg('请勾选需要入库的商品', {icon: 2});
                            return false;
                        }
                        var ids = [];
                        $.each(data, function (i, v) {
                            ids.push(v.id);
                        });
                        layer.confirm('确定添加？', function () {
                            ea.request.get({
                                url: 'addToLibrary',
                                data: {
                                    id: ids,
                                    library_id: library_id
                                },
                            }, function (res) {
                                ea.msg.success(res.msg)
                                window.close();
                            }, null, null);
                        });
                        return true;
                    })
                },
            });

            ea.listen();
        },
        add: function () {

            ea.listen();
        },
    };
    return Controller;
});